/*
 clsquare - closed loop simulation system
 Copyright (c) 2008, Neuroinformatics Group, Prof. Dr. Martin Riedmiller,
 University of Osnabrueck
 
 Author: Sascha Lange
 
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.
 * Neither the name of the <ORGANIZATION> nor the names of its
   contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 DAMAGE. 
 */

#include "pnm.h"
#include <fstream>

using namespace std;

bool writeGrayMap(const string filename, const double* observation, int width, int height)
{
  std::ofstream file(filename.c_str());
  if (file) {
    unsigned char buf[width*height];
    unsigned char* ptr = &buf[0];
    for (int i=width*height; i > 0; i--) {
      *ptr++ = static_cast<unsigned char>(*observation++ * 255 + .5);
    }
    file << "P5\n" << width << " " << height << " 255\n";
    file.write(reinterpret_cast<char*>(&buf[0]), width*height);
    file.close();
    return true;
  }
  return false;
}

static void skip(istream& in)
{
  char c;
  do {
    if (!in.get(c)) return;
  } while(isspace(c) || c=='\n' || c=='\r');
  if (c=='#') {                  // start of a comment
    do {                         // look for the end of the line
      if (!in.get(c)) return;
    } while(c!='\n');
    skip(in);                    // skip again at start of next line.
  }
  else                           // c was neither comment(#) nor space
    in.putback(c);               // so better put it back,
}

bool readGrayMap(const string filename, double* image, int* width, int* height)
{
  std::ifstream file(filename.c_str());
  if (!file) {
    return false;
  }
  
  // read header
  int type, maxCol;
  char c;
  
  skip(file);
  if (!file.get(c)) {
    file.close();
    return false;
  }
  if (c != 'P' && c != 'p') {
    file.close();
    return false;
  }
  
  file >> type;
  if (type != 5) {      // binary format?
    file.close();
    return false;
  }
  
  skip(file);
  file >> *width;
  skip(file);
  file >> *height;
  skip(file);
  file >> maxCol;         // 255 or smaller (bigger values not allowed)
  
  file.ignore(1);         // exactly one whitespace before start of binary data
  
  unsigned char buf[*width * *height];
  unsigned char* ptr = &buf[0];  
  
  double maxColD = maxCol;
  file.read(reinterpret_cast<char*>(&buf[0]), *width * *height);
  for (int i=*width**height; i > 0; i--) {
    *image++ = static_cast<unsigned char>(*ptr++) / maxColD;
  }
  
  file.close();
  
  return true;
}
